/*
 * PreferencesPane
 * 
 * Pollo Verde Software 2006
 * 
 * Este programa se distribuye segun la licencia GPL v.2 o posteriores y no
 * tiene garantias de ningun tipo. Puede obtener una copia de la licencia GPL o
 * ponerse en contacto con la Free Software Foundation en http://www.gnu.org
 */
package org.pvs.superpalitos.gui;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;

import org.pvs.superpalitos.SuperPalitos;

/**
 * @author Angel Luis Calvo Ortega
 */
public class PreferencesPane extends JPanel {
  private static final long serialVersionUID = 3258125856231403572L;
  private JButton aplicar;
  private JTextField textFieldUser1, textFieldUser2, jTFPuerto;
  private ColorBox colorBox1, colorBox2;
  private JComboBox jCBLaF;
  private JCheckBox jCBAnims, jCBSound;
  
  //private LookAndFeelInfo[] installedLaF;
  private LookAndFeelInfo[] lafs;
  private String[] lookAndFeelTags;
  private int currentLaF;
  
  public PreferencesPane(Color[] colors) {
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    initLookAndFeels();
    
    JPanel p = new JPanel(new GridLayout(4, 2));
    p.setBorder(new TitledBorder(" Jugadores "));
    p.add(new JLabel(" Nombre del jugador 1:"));
    textFieldUser1 = new JTextField();
    p.add(textFieldUser1);
    p.add(new JLabel(" Color del jugador 1:"));
    colorBox1 = new ColorBox(colors);
    p.add(colorBox1);

    p.add(new JLabel(" Nombre del jugador 2:"));
    textFieldUser2 = new JTextField();
    p.add(textFieldUser2);
    p.add(new JLabel(" Color del jugador 2:"));
    
    colorBox2 = new ColorBox(colors);
    p.add(colorBox2);
    add(p);
/*---*/
    p = new JPanel(new GridLayout(1, 2));
    p.setBorder(new TitledBorder(" Palitos Net "));
    p.add(new JLabel(" Puerto:"));
    jTFPuerto = new JTextField();
    p.add(jTFPuerto);
    add(p);
/*---*/    
    p = new JPanel(new GridLayout(3, 2));
    p.setBorder(new TitledBorder(" Visualizaci\u00F3n "));
    p.add(new JLabel(" Look & Feel:"));
    jCBLaF = new JComboBox(lookAndFeelTags);
    p.add(jCBLaF);
    p.add(new JLabel(" Animaciones:"));
    jCBAnims = new JCheckBox();
    p.add(jCBAnims);
    p.add(new JLabel(" Sonidos:"));
    jCBSound = new JCheckBox();
    p.add(jCBSound);
    add(p);
    
    //add(new JSeparator(JSeparator.HORIZONTAL));
    
    p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    aplicar = new JButton("Aplicar", SPFrame.II_ACCEPT);
    aplicar.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        aceptar_ActionPerformed(e);
      }
    });
    p.add(aplicar);
    add(p);
  }

  public void setData(String n1, String n2, Color c1, Color c2, int puerto, boolean anim, boolean sound) {
    textFieldUser1.setText(n1);
    colorBox1.setSelectedColor(c1);
    textFieldUser2.setText(n2);
    colorBox2.setSelectedColor(c2);
    jTFPuerto.setText(String.valueOf(puerto));
    jCBAnims.setSelected(anim);
    jCBSound.setSelected(sound);
  }
  
  private void aceptar_ActionPerformed(ActionEvent e) {
    String name1 = textFieldUser1.getText();
    String name2 = textFieldUser2.getText();
    try {
      int n3 = Integer.parseInt(jTFPuerto.getText());
      if(name1.length() > 0  && name2.length() > 0 && n3 > 1024 && n3 < 65535) {
        SuperPalitos m = SuperPalitos.getInstance();
        m.setJ1Name(name1);
        m.setJ1Color(colorBox1.getSelectedColor());
        m.setJ2Name(name2);
        m.setJ2Color(colorBox2.getSelectedColor());
        m.setPuerto(n3);
        m.setAnim(jCBAnims.isSelected());
        m.setSound(jCBSound.isSelected());
        
        if(jCBLaF.getSelectedIndex() != currentLaF) {
          currentLaF = jCBLaF.getSelectedIndex();
          UIManager.setLookAndFeel(lafs[currentLaF].getClassName());
          SuperPalitos.getInstance().changeLaF();
        }
      }
    } catch(NumberFormatException ex) {
      jTFPuerto.setText("0");
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    } catch (UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
    } catch (IllegalAccessException ex) {
      ex.printStackTrace();
    } catch (InstantiationException ex) {
      ex.printStackTrace();
    }
  }
  
  private void initLookAndFeels() {
  	//LookAndFeelInfo[] ins = UIManager.getInstalledLookAndFeels();
  	//LookAndFeel[] aux = UIManager.getAuxiliaryLookAndFeels();
  	
  	//lafs = new LookAndFeelInfo[ins.length + aux.length];
  	lafs = UIManager.getInstalledLookAndFeels();
    lookAndFeelTags = new String[lafs.length];
    
    for(int i = 0; i < lafs.length; i++ ) {
    	//lafs[i] = (i < ins.length) ? ins[i] : aux[i - ins.length].get
    	
      lookAndFeelTags[i] = lafs[i].getName();
      if(UIManager.getLookAndFeel().getName().equals(lafs[i].getName())) {
        currentLaF = i;
      }
    }
  }
}
